package com.dfbz.service;

import com.dfbz.dao.userDao;
import com.dfbz.entity.PageEntity;
import com.dfbz.entity.User;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserService {
    public static userDao userdao = new userDao();

    public static void updateUrl(Long id, String picUrl) {
        userDao.updateUrl(id,picUrl);
    }

    public static void update(User loginUser) {
        userdao.update(loginUser);
    }

    public static void updateLoginTime(Long id) {
        Date date=new Date();
        userdao.updateLoginTime(id);
    }

    public void save(User user) {
        //设置默认值  私密
        // 默认私密
        user.setIsSecret("0");
        // 真实姓名默认用户名
//        user.setRealName(user.getUsername());
        // 注册时间为当前
        user.setRegisterTime(new Date());
        //默认头像
//        user.setPic("https://www.baidu.com/favicon.ico");
        userdao.save(user);
    }

    public User findByEmail(String email) {
        return userdao.findByEmail(email);
    }

    public User findByUserName(String username) {
        return userdao.findByUserName(username);
    }

    /**
     * 用户分页+查询   -> 当前页： 当前页数据，总页数，总数据，页大小
     *
     * @param realName:模糊查询，根据用户真实姓名，查询结果。
     * @param currPage：当前页码
     * @return
     */
    public PageEntity<User> findPage(String realName, Integer currPage) {
        //声明一个实体类
        PageEntity<User> pageEntity = new PageEntity<>();
        //查询的总记录数
        Integer totalCount = userdao.countByRealName(realName);
        //页大小
        Integer pageSize = pageEntity.getPageSize();
        //其实索引
        Integer startIndex = (currPage - 1) * pageSize;
        // 查询当前页数据
        List<User> data = userDao.findPage(realName, startIndex, pageSize);

        // 总页数
    /*
        11---5---3
        15---5---3
        16---5---4
        21---5---5
     */
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize) + 1;
        pageEntity.setCurrPage(currPage);
        pageEntity.setData(data);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        pageEntity.setPageSize(pageSize);
        return pageEntity;
    }

    public static List<Map<String, Object>> findUserByDeptId(Long deptId) {
        return userdao.findUserByDeptId(deptId);
    }

    public User findByid(long id) {
        return userdao.findByid(id);
    }

    /**
     * 被查看数+1
     * @param id
     */

    public void inLook(long id) {
        userdao.inLook(id);
    }

    /**
     * 统计粉丝
     * @param id
     * @return
     */
    public Integer countFansByUserId(long id) {
        return userdao.countFansByUserId(id);
    }

    /**
     * 统计关注的数量
     * @param id
     * @return
     */

    public Integer countFoucsByUserId(long id) {
        return userdao.countFoucsByUserId(id);
    }

//    public  void update(User user) {
//
//        userdao.update(user);
//    }

    /**
     * 如果当前登陆的用户 已经关注了对方则  打钩。
     * @param id
     * @return
     */

    public List<Long> findFocusList(Long id) {
        return userDao.findFocusList(id);
    }

    public Integer focus(Long userId, Long focusId) {
        //首先判断该用户有无关注这个用户  如果关注则取关 反之
        List<Long> focusList=userdao.findFocusList(userId);
        if(focusList.contains(focusId)&&focusList!=null){
            userdao.unFocus(userId,focusId);
            return 0;
        }else {
            userdao.focus(userId,focusId);
            return 1;
        }
    }

    public PageEntity findFocusPage(Integer currPage, Long id) {
        PageEntity<Map<String,Object>> pageEntity=new PageEntity<>();
        //设置当前页
        pageEntity.setCurrPage(currPage);
        //获取页大小
        Integer pageSize = pageEntity.getPageSize();

        //计算开始索引
        Integer startIndex=(currPage-1)*pageSize;
        //当前页数据
        List<Map<String,Object>> data=userdao.findFocusPage(id,startIndex,pageSize);
        //总记录数
        Integer totalCount = userDao.countFoucsByUserId(id);
        //总页数
        Integer totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;

        pageEntity.setCurrPage(currPage);
        pageEntity.setData(data);
        pageEntity.setTotalCount(totalCount);
        pageEntity.setTotalPage(totalPage);
        return pageEntity;

    }

    public void register(User user) {
    }

    public User findByWxOpenId(String openid) {
        return userDao.findByWxOpenid(openid);
    }

    public void resist(String password, String email) {
        userdao.resist(password,email);
    }
}
